package com.water.wfo.component.api.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * packageName com.water.wfo.component.api.filter
 *
 * @author
 * @className RequestFilter(处以class为网)
 * @date 2025/1/16
 * @description T0D0
 */
@Slf4j
@Component
@Order(0)
@WebFilter(filterName="requestFilter",urlPatterns="/*")
public class RequestFilter implements Filter {

    static String SECURITY_HEADER = "method-name";
    private static ThreadLocal<ServletRequest> requestMap = new ThreadLocal<>();
    private static ThreadLocal<ServletResponse> responseMap = new ThreadLocal<>();
    private static ThreadLocal<String> remoteReqMethodMap = new ThreadLocal<>();
    private static ThreadLocal<String> remoteReqURLMap = new ThreadLocal<>();
    private static ThreadLocal<String> contextPathMap = new ThreadLocal<>();

    public RequestFilter(){

    }
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.debug("Request Filter start...");
        HttpServletRequest sreq;
        String uri;
        if(servletRequest instanceof HttpServletRequest){
            sreq = (HttpServletRequest) servletRequest;
            uri = sreq.getHeader(SECURITY_HEADER);
            String remoteReqUrl = sreq.getRequestURI();
            String contextPath = sreq.getContextPath();
            if(contextPath != null && !"/".equals(contextPath) && remoteReqUrl.startsWith(contextPath)){
                remoteReqUrl = remoteReqUrl.substring(contextPath.length());
            }

            remoteReqMethodMap.set(uri);
            remoteReqURLMap.set(remoteReqUrl);
            contextPathMap.set(contextPath);
        }
        requestMap.set(servletRequest);
        responseMap.set(servletResponse);

        try {
            log.debug("Request Filter doFilter ...");
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            log.debug("Request Filter doFilter end ...");
            this.setAllNull();
        }
    }

    @Override
    public void destroy() {

    }

    private void setAllNull() {
        remoteReqMethodMap.set(null);
        remoteReqURLMap.set(null);
        contextPathMap.set(null);
        requestMap.set(null);
        responseMap.set(null);
    }
    public static ThreadLocal<ServletRequest> getRequestMap() {
        return requestMap;
    }

    public static ThreadLocal<ServletResponse> getResponseMap() {
        return responseMap;
    }

    public static ThreadLocal<String> getRemoteReqMethodMap() {
        return remoteReqMethodMap;
    }

    public static ThreadLocal<String> getRemoteReqURLMap() {
        return remoteReqURLMap;
    }

    public static ThreadLocal<String> getContextPathMap() {
        return contextPathMap;
    }

    public static void setContextPathMap(ThreadLocal<String> contextPathMap) {
        RequestFilter.contextPathMap = contextPathMap;
    }
}
